{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tkinter\n",
    "from tkinter import simpledialog, colorchooser, filedialog\n",
    "from PIL import ImageGrab\n",
    "\n",
    "root = tkinter.Tk()\n",
    "root.title('My Paint by Dong FUGuo')  # 修正了字符串连接符\n",
    "root.geometry('800x600')  # 使用 geometry 替代设置 width 和 height\n",
    "\n",
    "canDraw = tkinter.IntVar(value=0)\n",
    "what = tkinter.IntVar(value=1)\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "foreColor = '#000000'\n",
    "backColor = '#FFFFFF'\n",
    "\n",
    "canvas = tkinter.Canvas(root, bg='white', width=800, height=600)\n",
    "canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)\n",
    "\n",
    "def onLeftButtonDown(event):\n",
    "    canDraw.set(1)\n",
    "    X.set(event.x)\n",
    "    Y.set(event.y)\n",
    "    if what.get() == 4:  # 这里需要定义 text 变量\n",
    "        text = simpledialog.askstring(\"Text\", \"Enter text:\")\n",
    "        if text:\n",
    "            canvas.create_text(event.x, event.y, text=text, fill=foreColor)\n",
    "\n",
    "canvas.bind('<Button-1>', onLeftButtonDown)\n",
    "\n",
    "lastDraw = None  # 使用 None 来标记 lastDraw，因为初始时没有绘制\n",
    "\n",
    "def onLeftButtonMove(event):\n",
    "    global lastDraw\n",
    "    if canDraw.get() == 0:\n",
    "        return\n",
    "    if what.get() == 1:\n",
    "        canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)\n",
    "        X.set(event.x)\n",
    "        Y.set(event.y)\n",
    "    elif what.get() == 2:\n",
    "        try:\n",
    "            canvas.delete(lastDraw)  # 修正拼写错误\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)\n",
    "    elif what.get() == 3:\n",
    "        try:\n",
    "            canvas.delete(lastDraw)  # 修正拼写错误\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, fill=backColor, outline=foreColor)\n",
    "    elif what.get() == 5:\n",
    "        canvas.create_rectangle(event.x-5, event.y-5, event.x+5, event.y+5, outline=backColor, fill=backColor)\n",
    "\n",
    "canvas.bind(\"<B1-Motion>\", onLeftButtonMove)\n",
    "\n",
    "def onLeftButtonUp(event):\n",
    "    global lastDraw\n",
    "    if what.get() == 2 or what.get() == 3:\n",
    "        if lastDraw is not None:\n",
    "            # 这里不需要再次绘制，因为已经在移动事件中绘制了\n",
    "            pass\n",
    "    canDraw.set(0)\n",
    "    lastDraw = None  # 重置 lastDraw\n",
    "\n",
    "canvas.bind('<ButtonRelease-1>', onLeftButtonUp)\n",
    "\n",
    "# 创建菜单栏\n",
    "menuBar = tkinter.Menu(root)\n",
    "root.config(menu=menuBar)\n",
    "\n",
    "menu = tkinter.Menu(menuBar, tearoff=0)\n",
    "menuBar.add_cascade(label=\"File\", menu=menu)\n",
    "\n",
    "def Save():\n",
    "    left = int(root.winfo_rootx())\n",
    "    top = int(root.winfo_rooty())\n",
    "    width = root.winfo_width()\n",
    "    height = root.winfo_height()\n",
    "    im = ImageGrab.grab((left, top, left + width, top + height))\n",
    "    filename = filedialog.asksaveasfilename(title='保存图片', filetypes=[('图片文件', '*.png')])\n",
    "    if filename:\n",
    "        if not filename.endswith('.png'):\n",
    "            filename += '.png'\n",
    "        im.save(filename)\n",
    "\n",
    "menu.add_command(label='Save', command=Save)\n",
    "\n",
    "def Clear():\n",
    "    for item in canvas.find_all():\n",
    "        canvas.delete(item)\n",
    "\n",
    "menu.add_command(label='Clear', command=Clear)\n",
    "menu.add_separator()\n",
    "\n",
    "menuType = tkinter.Menu(menu, tearoff=0)\n",
    "menu.add_cascade(label='Type', menu=menuType)\n",
    "\n",
    "def drawCurve():\n",
    "    what.set(1)\n",
    "menuType.add_command(label='Curve', command=drawCurve)\n",
    "\n",
    "def drawLine():\n",
    "    what.set(2)\n",
    "menuType.add_command(label='Line', command=drawLine)\n",
    "\n",
    "def drawRectangle():\n",
    "    what.set(3)\n",
    "menuType.add_command(label='Rectangle', command=drawRectangle)\n",
    "\n",
    "def drawText():\n",
    "    global text\n",
    "    text = simpledialog.askstring(\"Text\", \"Enter text:\")\n",
    "    if text:\n",
    "        # 这里不需要直接调用 drawText，而是应该设置 what 变量并可能在按下时处理\n",
    "        what.set(4)\n",
    "        # 假设在按下左键时已经处理了文本绘制\n",
    "menuType.add_command(label='Text', command=drawText)\n",
    "\n",
    "def chooseForeColor():\n",
    "    global foreColor\n",
    "    foreColor = colorchooser.askcolor()[1]\n",
    "menuType.add_command(label='Choose Foreground Color', command=chooseForeColor)\n",
    "\n",
    "def chooseBackColor():\n",
    "    global backColor\n",
    "    backColor = colorchooser.askcolor()[1]\n",
    "menuType.add_command(label='Choose Background Color', command=chooseBackColor)\n",
    "\n",
    "def onErase():\n",
    "    what.set(5)\n",
    "menuType.add_command(label='Erase', command=onErase)\n",
    "def onRightButton(event):\n",
    "    menu.post(event.x_root, event.y_root)\n",
    "\n",
    "canvas.bind('<Button-3>', onRightButton)  # 修正事件绑定\n",
    "\n",
    "def drawCircle():\n",
    "    what.set(6)\n",
    "\n",
    "menuType.add_command(label='Circle', command=drawCircle)\n",
    "\n",
    "def onLeftButtonMove(event):\n",
    "    global lastDraw\n",
    "    if canDraw.get() == 0:\n",
    "        return\n",
    "    if what.get() == 1:\n",
    "        canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)\n",
    "        X.set(event.x)\n",
    "        Y.set(event.y)\n",
    "    elif what.get() == 2:\n",
    "        try:\n",
    "            canvas.delete(lastDraw)  # 修正拼写错误\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)\n",
    "    elif what.get() == 3:\n",
    "        try:\n",
    "            canvas.delete(lastDraw)  # 修正拼写错误\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, fill=backColor, outline=foreColor)\n",
    "    elif what.get() == 5:\n",
    "        canvas.create_rectangle(event.x-5, event.y-5, event.x+5, event.y+5, outline=backColor, fill=backColor)\n",
    "    elif what.get() == 6:\n",
    "        try:\n",
    "            canvas.delete(lastDraw)  # 修正拼写错误\n",
    "        except Exception as e:\n",
    "            pass\n",
    "        lastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y, outline=foreColor, fill=backColor)\n",
    "\n",
    "canvas.bind(\"<B1-Motion>\", onLeftButtonMove)\n",
    "\n",
    "root.mainloop()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
